using System;
using Smallpointer.Configuration.Common;

namespace Smallpointer.Data.Common
{
    internal sealed class DataConfig : IDisposable
    {
        private readonly ConfigurationReader ConfigReader;
        public ConnType DBType { get; private set; }
        public string Connstring { get; private set; }
        public DataSpace DBSapce { get; private set; }
        public DataConfig()
        {
            ConfigReader = new ConfigurationReader();
            InitNeedConfig();
        }

        private void InitNeedConfig()
        {
            //init database type
            var conntype = ConfigReader.Appsettings("conntype");
            object temp;
            try
            {
                temp = Enum.Parse(typeof(ConnType), conntype);
            }
            catch (Exception ex)
            {
                throw new ArgumentOutOfRangeException("Invalidate Parameter for the data property attribute, please check the entity map xml.", ex);
            }

            DBType = temp == null ? ConnType.None : (ConnType)temp;
            //init connection string
            Connstring = ConfigReader.Appsettings("connstr");

            //init database type
            var space = ConfigReader.Appsettings("dataspace");
            try
            {
                temp = Enum.Parse(typeof(DataSpace), space);
            }
            catch (Exception ex)
            {
                throw new ArgumentOutOfRangeException("Invalidate Parameter for the data property attribute, please check the entity map xml.", ex);
            }

            DBSapce = temp == null ? DataSpace.Resource : (DataSpace)temp;
        }

        public void Dispose()
        {

        }
    }
}